import os
import numpy as np

ark_path = "/ai-cephfs/Share/hyc_share/native_std_us_english/combine-500h-data/cmvn_500h_confidence_usenglish.ark.txt"
old_train_scp = "/home/changhengyi/gru-pytorch/train.scp"
old_test_scp = "/home/changhengyi/gru-pytorch/test.scp"
feat_dir = "/ai-cephfs/Share/hyc_share/native_std_us_english/combine-500h-data/features/"
label_file = "/ai-cephfs/Share/zps_share/native_en/combine_data/48phones.ali2phones.txt"
train_scp = "/ai-cephfs/Share/hyc_share/native_std_us_english/combine-500h-data/train.scp"

audio_num = 202557

# def split_ark_features():
#     print("-----------------------------------------------------")
#     print("split_ark_features...")
#     cnt = 1
#     with open(ark_path, "r") as f:
#         with open(feat_scp, "w") as f2:
#             line = f.readline()
#             while line:
#                 if line.strip().endswith('['):
#                     audio_id = line.strip().split()[0]
#                     line = f.readline()
#                     feat_path = feat_dir + audio_id + "-feat.txt"
#                     with open(feat_path, "w") as fea:
#                         while len(line.split()) >= 80:
#                             fea.write(line.strip(" ]\n") + "\n")
#                             line = f.readline()
#                         print("{}/{} finished        \r".format(cnt, audio_num),end="")
#                         f2.write("{} {}\n".format(audio_id, feat_path))
#                         cnt += 1
#                 else:
#                     break
#             print()
def ali2phone_id(x):
    returint(x)

def generate_scp():
    cnt, total_num = 1, 202557
    feat_dict = {}
    with open(old_train_scp, "r") as f:
        line = f.readline()
        while line:
            utt_id, feat_path, _ = line.strip().split()
            feat_dict[utt_id] = feat_path
            print("{}/{}      \r".format(cnt, total_num), end="")
            cnt += 1
            line = f.readline()

    print()
    cnt = 1

    with open(old_test_scp, "r") as f:
        line = f.readline()
        while line:
            utt_id, feat_path, _ = line.strip().split()
            feat_dict[utt_id] = feat_path
            print("{}/{}      \r".format(cnt, total_num), end="")
            cnt += 1
            line = f.readline()

    print()
    cnt = 1
    
    label_dict = {}
    with open(label_file, "r") as f:
        line = f.readline()
        while line:
            utt_id = line.strip().split()[0]
            label_list = line.strip().split()[1:]
            label_list = [ max(0, int(x) - 2) for x in label_list ]
            label_dict[utt_id] = np.array(label_list)
            print("{}/{}      \r".format(cnt, total_num), end="")
            cnt += 1
            line = f.readline()

    print("-----------------------------------")
    
    cnt, err_cnt = 1, 1
    label_dir = "/ai-cephfs/Share/hyc_share/native_std_us_english/combine-500h-data/label/"
    with open(train_scp, "w") as f:
        for utt_id in label_dict:
            label = label_dict[utt_id]
            # feat = np.load(feat_dict[utt_id])
            label_path = label_dir + utt_id + "-label.npy"
            np.save(label_path, label)
            if utt_id in feat_dict:
                f.write("{} {} {}\n".format(utt_id, feat_dict[utt_id], label_path))
                print("{}/{}      \r".format(cnt, total_num), end="")
                cnt += 1
            else:
                err_cnt += 1

    print()
    print("err_cnt:",err_cnt)



if __name__ == "__main__":
    # split_ark_features()
    generate_scp()